home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / MovieShell / Mac Framework / MacFramework.c < prev    next >
Encoding:
Text File  |  1995-05-25  |  21.6 KB  |  885 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        MacFramework.c
  3.  
  4.     Contains:    Basic Macintosh Functions for Window, Menu handling and similar things.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         12/20/94    khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include <SegLoad.h>
  19. #include <ToolUtils.h>
  20. #include <Devices.h>
  21. #include <Fonts.h>
  22.  
  23. #include "DTSQTUtilities.h"
  24. #include "AppConfiguration.h"
  25. #include "MacFramework.h"
  26.  
  27.  
  28. // WINDOW DEFINITIONS
  29. const Rect kDefaultWinRect;
  30. Rect kLimitRect = {0, 0, 480, 640};                            //Max size for any window
  31. long gMCFlags = kMCFlags;
  32.  
  33.  
  34. // GLOBALS
  35. Boolean             gQuitFlag = false;                                // Flag that keeps track of termination state.
  36. unsigned long     gWNEsleep = kWNEDefaultSleep;            // WaitNextEvent sleep time.
  37. Str255             gWindowTitle = "\pUntitled";            // Default name for created windows.
  38. GrowZoneUPP    gAppGrowZoneUPP;                            // Our grow zone callback.    
  39. Boolean                gJustOneMovie = false;                        // Flag for indication that one movie has been created,
  40.                                                                                 // in other words a limitation flag for single movie environment.
  41.  
  42. // PURE MAC TOOLBOX FUNCTIONS
  43.  
  44. // ______________________________________________________________________
  45. void InitMacEnvironment(long nMasters)
  46. {
  47.     long i;
  48.     MaxApplZone();
  49.     
  50.     for(i = 0; i <nMasters; i++)
  51.         MoreMasters();
  52.     
  53.     InitGraf(&qd.thePort);
  54.     InitFonts();
  55.     InitWindows();
  56.     InitMenus();
  57.     FlushEvents(everyEvent, 0);
  58.     TEInit();
  59.     InitCursor();
  60.     InitDialogs(NULL);
  61.     
  62.     // Additional goodie, install a growzone proc warning about low memory situation
  63.      gAppGrowZoneUPP = NewGrowZoneProc(AppGrowZoneCallback);
  64.      SetGrowZone(gAppGrowZoneUPP);
  65. }
  66.  
  67.  
  68. // ______________________________________________________________________
  69. void InitStack(long extraStackSpace)
  70. {
  71.     Ptr size = GetApplLimit();
  72.     SetApplLimit(size - extraStackSpace);    // make room on the stack
  73. }
  74.  
  75.  
  76. // ______________________________________________________________________
  77. pascal void AppGrowZoneCallback(void)
  78. {
  79.     long         theA5;
  80.     Size        tempSize, availMem;
  81.     
  82.     theA5 = SetCurrentA5();
  83.     
  84.     availMem = MaxMem(&tempSize);
  85.  
  86.     if(availMem < kAvailableMem)
  87.     {
  88.         ShowWarning("\pWe are running out of memory, increase the application heap! Exiting the application.", 0);
  89.         ExitToShell();
  90.     }
  91.     
  92.     SetA5(theA5);
  93. }
  94.  
  95.  
  96. // ______________________________________________________________________
  97. Boolean InitMenubar(void)
  98. {
  99.     Handle aMenuHandle = NULL;
  100.     
  101.     aMenuHandle = GetNewMBar(mMenubar); DebugAssert(aMenuHandle != NULL);
  102.     if(aMenuHandle == NULL)
  103.     {
  104.         ShowWarning("\pCould not find the Menubar resource!", 0);
  105.         return false;
  106.     }
  107.     
  108.     SetMenuBar(aMenuHandle);
  109.     DisposeHandle(aMenuHandle);  DebugAssert(MemError() == noErr);
  110.     
  111.     AddResMenu(GetMHandle(mApple), 'DRVR');
  112.  
  113.     DrawMenuBar();
  114.     return true;
  115. }
  116.  
  117.  
  118. // ______________________________________________________________________
  119. void HandleMenuCommand(long theMenuResult)
  120. {
  121.     short             aMenuID, aMenuItem;
  122.     Str255            daName;
  123.     
  124.     aMenuID = HiWord(theMenuResult);
  125.     aMenuItem = LoWord(theMenuResult);
  126.     
  127.     switch(aMenuID)
  128.     {
  129.         // APPLE MENU
  130.         case mApple:
  131.             switch(aMenuItem)
  132.             {
  133.                 case iAbout:    // about box
  134.                     ShowAboutDialogBox();     
  135.                     break;
  136.                 
  137.                 default:     // Apple menu handling
  138.                     GetItem(GetMHandle(mApple), aMenuItem, daName);
  139.                     (void)OpenDeskAcc(daName);
  140.                     break;
  141.             } // end switch(aMenuItem)
  142.             break;
  143.  
  144.         // FILE MENU            
  145.         case mFile:
  146.             switch(aMenuItem)
  147.             {
  148.                 case iNew:
  149.                     {
  150.  
  151. #ifdef ONEMOVIELIMIT
  152.                         if(gJustOneMovie == false)
  153. #endif
  154.                         {
  155.                             if ( !DoCreateNewMovie())
  156.                             {
  157.                                 SysBeep(kDefaultSysBeep);
  158.                                 ShowWarning("\pCould not create a new movie!", 0);
  159.                                 break;
  160.                             }
  161.                         }
  162. #ifdef ONEMOVIELIMIT
  163.                         gJustOneMovie = true;
  164. #endif
  165.                     }
  166.                     break;
  167.                     
  168.                 case iOpen:
  169. #ifdef ONEMOVIELIMIT
  170.                     if(gJustOneMovie == false)
  171. #endif
  172.                     {
  173.                         if (!DoCreateMovieWindow( NULL) )
  174.                         {
  175.                             ShowWarning("\pCould not create a new movie window!", 0);
  176.                             SysBeep(kDefaultSysBeep); 
  177.                             break;
  178.                         }
  179.                     }
  180.                     
  181. #ifdef ONEMOVIELIMIT
  182.                     gJustOneMovie = true;
  183. #endif
  184.                     break;
  185.                 
  186.                 case iClose:
  187.                     DoDestroyMovieWindow( FrontWindow() );
  188. #ifdef ONEMOVIELIMIT
  189.                     gJustOneMovie = false;
  190. #endif
  191.                     break;
  192.                 
  193.                 case iSave:
  194.                     {
  195.                         if( !DoUpdateMovieFile( FrontWindow()) )
  196.                         {
  197.                             SysBeep(kDefaultSysBeep);
  198.                             ShowWarning("\pCould not save the movie file!", 0);
  199.                              break;
  200.                         }
  201.                     }
  202.                     break;
  203.                     
  204.                 case iSaveAs:
  205.                     {
  206.                         MovieController mc;
  207.                     
  208.                         mc = GetMCFromFrontWindow();
  209.                         if(mc == NULL) 
  210.                         {
  211.                             SysBeep(kDefaultSysBeep); 
  212.                             break;
  213.                         }
  214.                         
  215.                          if( QTUSaveMovie(MCGetMovie(mc)) != noErr)
  216.                          {
  217.                              SysBeep(kDefaultSysBeep);
  218.                              ShowWarning("\pCould not save the movie file!", 0);
  219.                              break;
  220.                          }
  221.                      }
  222.                     break;
  223.                                     
  224.                 case iPrint:
  225.                     {
  226.                         MovieController mc;
  227.                         OSErr anErr = noErr;
  228.  
  229.                         mc = GetMCFromFrontWindow();
  230.                         if(mc != NULL)
  231.                         {
  232.                             anErr = QTUPrintMoviePICT( MCGetMovie(mc), kDefaultX, kDefaultY, kPrintFrame); 
  233.                             if(anErr != noErr)
  234.                             {
  235.                                 ShowWarning("\pCould not print!", anErr);
  236.                                 SysBeep(kDefaultSysBeep);
  237.                             }
  238.                         }
  239.                         else
  240.                                 SysBeep(kDefaultSysBeep);
  241.                         break;
  242.                     }
  243.  
  244.                 case iQuit:
  245.                     {
  246.                         gQuitFlag = true;
  247.                         break;
  248.                     }
  249.  
  250.             } // end switch(aMenuItem), mFile
  251.             break;
  252.     
  253.         
  254.         // EDIT MENU
  255.         // Provide the default controller cut, copy and paste functionality.    
  256.         case mEdit:
  257.         {
  258.             Movie aMovie = NULL;
  259.             MovieController mc;
  260.             
  261.             mc = GetMCFromFrontWindow();
  262.             if (mc == NULL) break;
  263.             
  264.             switch(aMenuItem)
  265.             {
  266.                 case iUndo: MCUndo(mc); break;
  267.                 
  268.                 case iCut: aMovie = MCCut(mc); break;
  269.                 
  270.                 case iCopy: aMovie = MCCopy(mc); break;
  271.                 
  272.                 case iPaste: MCPaste(mc, NULL); break;
  273.                 
  274.                 case iClear: MCClear(mc); break;
  275.                 
  276.                 case iSelectAll:  
  277.                     if(QTUSelectAllMovie(mc) != noErr)
  278.                         SysBeep(kDefaultSysBeep);
  279.                     break;
  280.             } // end switch(aMenuItem)
  281.             
  282.             if(aMovie)
  283.             {
  284.                 PutMovieOnScrap(aMovie, 0);
  285.                 DisposeMovie(aMovie);  DebugAssert(MemError() == noErr);
  286.             }
  287.             break;
  288.         } // end case mEdit
  289.  
  290.  
  291.     default:
  292.         HandleApplicationMenu(aMenuID, aMenuItem);
  293.         break;
  294.     } // end switch(aMenuID)
  295.     
  296.     HiliteMenu(0);
  297. }
  298.  
  299.  
  300. // ______________________________________________________________________
  301. void AdjustMenus(void)
  302. {
  303.     WindowRef             aWindow;
  304.     MovieController    mc;
  305.     WindowObject        aWindowObject;
  306.  
  307.         
  308. #ifdef ONEMOVIELIMIT
  309.                 if(gJustOneMovie == true)
  310.                 {
  311.                     DisableItem(GetMHandle(mFile), iNew);
  312.                     DisableItem(GetMHandle(mFile), iOpen);
  313.                 }
  314.                 else if(gJustOneMovie == false)
  315.                 {
  316.                     EnableItem(GetMHandle(mFile), iNew);
  317.                     EnableItem(GetMHandle(mFile), iOpen);
  318.                 }
  319. #endif
  320.     
  321.     aWindow = FrontWindow();
  322.  
  323.     if(aWindow != NULL)
  324.     {
  325.         // Enable the close entry of we have windows = movies.
  326.         EnableItem( GetMHandle(mFile), iClose);
  327.         
  328.         // Handle the edit menu.
  329.         if( (aWindowObject = (WindowObject)GetWRefCon(aWindow) ) != NULL)
  330.         {
  331.             mc = (**aWindowObject).controller;
  332.             if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL))
  333.             {
  334.                 MCSetUpEditMenu(mc, 0L, GetMHandle(mEdit));
  335.                 EnableItem(GetMHandle(mEdit), iSelectAll);
  336.                 EnableItem(GetMHandle(mFile), iSave);
  337.                 EnableItem(GetMHandle(mFile), iSaveAs);
  338.                 EnableItem(GetMHandle(mFile), iClose);
  339.                 EnableItem(GetMHandle(mFile), iPrint);
  340.             }
  341.         }
  342.     } // end if(aWindow != NULL)
  343.     else 
  344.     {
  345.         DisableItem(GetMHandle(mFile), iSave);
  346.         DisableItem(GetMHandle(mFile), iSaveAs);
  347.         DisableItem(GetMHandle(mFile), iClose);
  348.         DisableItem(GetMHandle(mFile), iPrint);
  349.         
  350.         DisableItem(GetMHandle(mEdit), iCut);
  351.         DisableItem(GetMHandle(mEdit), iCopy);
  352.         DisableItem(GetMHandle(mEdit), iPaste);
  353.         DisableItem(GetMHandle(mEdit), iUndo);
  354.         DisableItem(GetMHandle(mEdit), iClear);
  355.         DisableItem(GetMHandle(mEdit), iSelectAll);
  356.         
  357.     }
  358.     
  359.     AdjustApplicationMenus();                    // fix any specific app menus as well.
  360. }
  361.  
  362.  
  363. // ______________________________________________________________________
  364. void MainEventLoop(void)
  365. {
  366.     EventRecord             anEvent;
  367.     WindowRef            whichWindow, aWindow;
  368.     Boolean                    aMovieEvent;
  369.     short                    aWindowPart;
  370.     Rect                        aScreenRect;
  371.     Rect                        aRefreshArea;
  372.     Point                        aPoint  = {100, 100};
  373.     WindowObject        aWindowObject;
  374.     MovieController    mc;
  375.     
  376.     while(!gQuitFlag)
  377.     {
  378.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  379.         
  380. #ifdef USESIOUX
  381.         SIOUXHandleOneEvent(&anEvent);
  382. #endif USESIOUX
  383.  
  384.         AdjustMenus();
  385.         aMovieEvent = false;
  386.         
  387.         if( (whichWindow = FrontWindow() ) != NULL)
  388.             DoIdle(whichWindow);
  389.  
  390.         // First, let the movie controller have access to the event.
  391.         for( aWindow = FrontWindow(); aWindow != NULL ; aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow)
  392.             if(( aWindowObject = (WindowObject)GetWRefCon(aWindow)) != NULL)         
  393.                 if((IsWindowObjectOurs(aWindowObject)) && ( (mc = (**aWindowObject).controller) != NULL) ) 
  394.                     if(MCIsPlayerEvent(mc, &anEvent)) 
  395.                         aMovieEvent = true ;
  396.                     
  397.     // Then, if this wasn't a movie controller event, pass it on to the case statement that dispatches the event
  398.     // to the right function.
  399.     if(!aMovieEvent)
  400.     {
  401.         switch(anEvent.what)
  402.         {
  403.             case mouseDown:
  404.                 aWindowPart = FindWindow(anEvent.where, &whichWindow);
  405.  
  406.                 // Window related events:            
  407.                 switch(aWindowPart)
  408.                 {
  409.                     case inMenuBar:
  410.                         HandleMenuCommand(MenuSelect(anEvent.where));
  411.                         break;
  412.                         
  413.                     case inDrag:
  414.                     {
  415.                         Rect                         aRect;
  416.                         Movie                    aMovie = NULL;
  417.                         MovieController     mc = NULL;
  418.                         WindowObject         aWindowObject = NULL;
  419.                         
  420.                         aWindowObject = (WindowObject)GetWRefCon(whichWindow);
  421.                         mc = (**aWindowObject).controller;
  422.                         if (! (IsWindowObjectOurs(aWindowObject)) && (mc == NULL))
  423.                             break;
  424.                             
  425.                         aMovie = MCGetMovie(mc);
  426.                         
  427.                         GetMovieBox(aMovie, &aRect);
  428.                         aScreenRect = (**GetGrayRgn()).rgnBBox;
  429.                         DragAlignedWindow(whichWindow, anEvent.where, &aScreenRect, &aRect, NULL);
  430.                     }  // end case inDrag;    
  431.                         break;
  432.                         
  433.                     case inContent:
  434.                         SelectWindow(whichWindow);
  435.                         HandleContentClick(whichWindow, &anEvent);
  436.                         break;
  437.                     
  438.                     case inGoAway:
  439.                         // if the window is closed, dispose the movie, the controller and the window
  440.                         if( TrackGoAway(whichWindow, anEvent.where) )
  441.                             DoDestroyMovieWindow(whichWindow);
  442. #ifdef ONEMOVIELIMIT
  443.                             gJustOneMovie = false;
  444. #endif
  445.                         break;
  446.                 } // end switch(aWindowPart):
  447.                 break;
  448.  
  449.                 // System level events:
  450.                 case updateEvt:
  451.                     whichWindow = (WindowRef)anEvent.message;
  452.                     aRefreshArea = ((**(whichWindow->visRgn)).rgnBBox);
  453.                     DoUpdateWindow(whichWindow, &aRefreshArea);
  454.                     break;
  455.                     
  456.                 case keyDown:
  457.                 case autoKey:
  458.                     HandleKeyPress(&anEvent);
  459.                     break;
  460.                 
  461.                 case diskEvt:
  462.                     if(HiWord(anEvent.message) != noErr)
  463.                         (void)DIBadMount(aPoint, anEvent.message);
  464.                     break;
  465.                 
  466.                 case activateEvt:
  467.                     whichWindow = (WindowRef)anEvent.message;
  468.                     
  469.                      if ( IsAppWindow(whichWindow) )
  470.                     {
  471.                         DoActivateWindow(whichWindow, ((anEvent.modifiers & activeFlag) != 0 ));
  472.                     }
  473.                     break;
  474.                     
  475.                 case osEvt:
  476.                     switch(( anEvent.message > 24) & 0x00FF )        // get high byte of word
  477.                     {
  478.                         case suspendResumeMessage:
  479.                             if( FrontWindow() )
  480.                             {
  481.                                 DoActivateWindow(FrontWindow(), !((anEvent.message & resumeFlag) == 0));
  482.                             }
  483.                             break;
  484.                         
  485.                         case mouseMovedMessage:
  486.                             break;
  487.                     } // end switch(anEvent.message > 24) & 0x00FF)    
  488.                     break;
  489.                 
  490.                 case nullEvent:
  491.                     if(( whichWindow = FrontWindow() ) != NULL)
  492.                         DoIdle(whichWindow);
  493.                     break;
  494.         } // end switch(anEvent.what)
  495.     } // end if(!aMovieEvent)    
  496.     } // end while(!gQuitFlag)
  497. }
  498.  
  499.  
  500. // ______________________________________________________________________
  501. Boolean IsAppWindow(WindowRef theWindow)
  502. {
  503.     short aWindowKind;
  504.     
  505.     if (theWindow == NULL)
  506.         return false;
  507.     else
  508.     {
  509.         aWindowKind = ((WindowPeek)theWindow)->windowKind;
  510.         return ( (aWindowKind >= userKind) || (aWindowKind == dialogKind) );
  511.     }
  512. }
  513.  
  514.  
  515. // ______________________________________________________________________
  516. WindowObject CreateWindowObject(WindowRef theWindow)
  517. {
  518.     WindowObject aWindowObject = NULL;
  519.     
  520.     // WindowObjectRecord = 90 bytes (good to know if chasing for handles in the heap).
  521.     aWindowObject = (WindowObject)NewHandle(sizeof(WindowObjectRecord));
  522.     
  523.     if(aWindowObject != NULL)
  524.     {
  525.         (**aWindowObject).controller = NULL;
  526.         (**aWindowObject).ObjectType = kMovieControllerObject;
  527.         
  528.         SetWRefCon(theWindow, (long)aWindowObject);        // store a ref to the record/handle into the window
  529.     }
  530.     
  531.     return aWindowObject;
  532. }
  533.  
  534.  
  535. // ______________________________________________________________________
  536. void HandleKeyPress(EventRecord *theEvent)
  537. {
  538.     char aKey;
  539.     
  540.     aKey = theEvent->message & charCodeMask;
  541.     
  542.     if(theEvent->modifiers & cmdKey)        // command key down?
  543.     {
  544.         HandleMenuCommand(MenuKey(aKey));
  545.     }
  546. }
  547.  
  548.  
  549. // ______________________________________________________________________
  550. void ShowAboutDialogBox(void)
  551. {
  552.     DialogPtr aDialog;
  553.     short         itemHit;
  554.     FontInfo    aFontInfo;
  555.     GrafPtr        aSavedPort;
  556.     
  557.     GetPort(&aSavedPort);
  558.     aDialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) - 1L); DebugAssert(aDialog != NULL);
  559.     SetPort(aDialog);
  560.  
  561.     // Change font to Geneva, 9pt, bold, just for the sake of it...
  562.     TextFont(applFont); TextSize(9); TextFace(bold);
  563.     GetFontInfo(&aFontInfo);
  564.     
  565.     (*((DialogPeek)aDialog)->textH)->txFont = applFont;
  566.     (*((DialogPeek)aDialog)->textH)->txSize = 9;
  567.     (*((DialogPeek)aDialog)->textH)->lineHeight = aFontInfo.ascent + aFontInfo.descent + aFontInfo.leading;
  568.     (*((DialogPeek)aDialog)->textH)->fontAscent = aFontInfo.ascent;
  569.  
  570.     SetDialogDefaultItem(aDialog, 1);
  571.         
  572.     do
  573.     {
  574.         ModalDialog(NULL, &itemHit);
  575.     } while(itemHit != ok);
  576.     
  577.     SetPort(aSavedPort);
  578.     DisposeDialog(aDialog);  DebugAssert(MemError() == noErr);
  579. }
  580.  
  581.  
  582. // ______________________________________________________________________
  583. void ShowWarning(Str255 theMessage, OSErr theErr)
  584. {
  585.     Str255 errString;
  586.     
  587.     NumToString(theErr, errString);
  588.     ParamText("\pWarning!", theMessage, theErr ? errString:  NULL, NULL);
  589.     Alert(kAlertError, NULL);
  590. }
  591.  
  592.  
  593. // MOVIE RELATED TOOLBOX FUNCTIONS
  594.  
  595. // ______________________________________________________________________
  596. MovieController  GetMCFromFrontWindow(void)
  597. {
  598.     MovieController     mc = NULL;
  599.     WindowRef             aWindow = NULL;
  600.     WindowObject        aWindowObject = NULL;
  601.     Movie                    aMovie = NULL;
  602.     OSErr                    anErr = noErr;
  603.     OSType                    aType = NULL;
  604.  
  605.     if( ( aWindow = FrontWindow() ) == NULL )
  606.         return NULL;
  607.  
  608.     if( !IsAppWindow(aWindow) )
  609.         return NULL;
  610.             
  611.     aWindowObject = (WindowObject)GetWRefCon(aWindow);
  612.     if(aWindowObject == NULL)
  613.         return NULL;
  614.         
  615.     MoveHHi((Handle)aWindowObject); HLock((Handle)aWindowObject);
  616.  
  617.     // Test if this is indeed a movie controller, and not an otherwise valid pointer (non-NULL value)
  618.     if(!IsWindowObjectOurs(aWindowObject))
  619.         return NULL;
  620.         
  621.     mc = (**aWindowObject).controller;
  622.     HUnlock((Handle)aWindowObject);
  623.     
  624.     return mc;
  625. }
  626.  
  627.  
  628. // ______________________________________________________________________
  629. Boolean IsWindowObjectOurs(WindowObject theObject)
  630. {
  631.     OSType        aType = NULL;
  632.     
  633.     aType = (**theObject).ObjectType;
  634.     if(aType == kMovieControllerObject)
  635.         return true;
  636.     else return false;
  637. }
  638.  
  639.  
  640.  
  641. // ______________________________________________________________________
  642. Boolean DoCreateNewMovie(void)
  643. {
  644.     Movie aMovie = NULL;
  645.     
  646.     aMovie = NewMovie(newMovieActive); DebugAssert(aMovie != NULL);
  647.     if(aMovie == NULL)
  648.         return false;
  649.     
  650.     if(!DoCreateMovieWindow(aMovie))
  651.         return false;
  652.     else    
  653.         return true;
  654. }
  655.  
  656.  
  657. // ______________________________________________________________________
  658. Boolean DoCreateMovieWindow(Movie theMovie)
  659. {
  660.     Rect                         aRect = kDefaultWinRect;
  661.     WindowRef            aWindow = NULL;
  662.     MovieController    mc = NULL;
  663.     WindowObject        aWindowObject = NULL;
  664.     GrafPtr                    aSavedPort;
  665.     short                    aRefNum;
  666.     short                    aResID;
  667.     FSSpec                    aFileFSSpec;
  668.     
  669.     aFileFSSpec.vRefNum = 0;            // we want to use the FSSpec later
  670.  
  671.     GetPort(&aSavedPort);
  672.     aWindow = CreateMovieWindow(&aRect, gWindowTitle);
  673.     SetPort((GrafPtr) aWindow);
  674.     
  675.     if(aWindow == NULL)
  676.         return false;
  677.     
  678.     aWindowObject = CreateWindowObject(aWindow);
  679.     if(aWindowObject == NULL)
  680.         return false;
  681.  
  682. //If we don't get a movie, call the internal QTUGetMovie that will get us one.
  683.     if(theMovie == NULL)
  684.     {
  685.         theMovie = QTUGetMovie(&aFileFSSpec, &aRefNum, &aResID);  DebugAssert(theMovie != NULL);
  686.         if(theMovie == NULL)  // User selected cancel, or otherwise something bad happened.
  687.             return false;
  688.             
  689.         // Add the FSSpec, refnum and resid values to the window object (we need these when we save the movie).
  690.         (**aWindowObject).FileFSSpec = aFileFSSpec;
  691.         (**aWindowObject).FileRefNum = aRefNum;
  692.         (**aWindowObject).FileResID = aResID;
  693.  
  694.         // Get movie title and set this to the window title.
  695.         SetWTitle(aWindow, aFileFSSpec.name);
  696.     }
  697.     
  698.     SetMovieGWorld(theMovie, (CGrafPtr)aWindow, 0);        // make sure the movie uses the window GWorld at all situations
  699.     mc = SetupMovieWindowWithController(theMovie, aWindow);
  700.     
  701.     ShowWindow(aWindow);
  702.     SelectWindow(aWindow);                                    // make it front-most as it's just created
  703.     InvalRect( &((GrafPtr)aWindow)->portRect);
  704.     
  705.     MCEnableEditing(mc, true);                                // enable the default movie controller editing
  706.     
  707.     SetPort(aSavedPort);
  708.     return true;
  709. }
  710.  
  711.  
  712. // ______________________________________________________________________
  713. MovieController SetupMovieWindowWithController(Movie theMovie, WindowRef theWindow)
  714. {
  715.     MovieController     mc;
  716.     Rect                        aRect;
  717.     GrafPtr                    aSavedPort;
  718.     WindowObject        aWindowObject;
  719.     short                    aMovieWidth;
  720.     short                    aMovieHeight;
  721.     
  722.     DebugAssert(theMovie != NULL); 
  723.     DebugAssert(theWindow != NULL);
  724.     
  725.     aWindowObject = (WindowObject)GetWRefCon(theWindow);            // Get our window specific data.
  726.     if(!IsWindowObjectOurs(aWindowObject))
  727.         return NULL;                                                                                // Quick sanity test of the window created.
  728.     GetPort(&aSavedPort);
  729.     SetPort( (GrafPtr)theWindow);
  730.     
  731. // Resize the movie bounding rect.
  732.     GetMovieBox(theMovie, &aRect);     SetMovieBox(theMovie, &aRect);
  733.  
  734. // Create the movie controller.    
  735.     mc = NewMovieController(theMovie, &aRect, gMCFlags);
  736.     if(mc == NULL)
  737.         return NULL;
  738.     MCGetControllerBoundsRect(mc, &aRect);
  739.     
  740. // Add grow box for the movie controller and also an action filter that resizes the controllers
  741.     MCDoAction(mc, mcActionSetGrowBoxBounds, &kLimitRect);
  742.     MCSetActionFilterWithRefCon(mc, 
  743.                                                     NewMCActionFilterWithRefConProc(QTUResizeMCActionFilter),
  744.                                                     (long) theWindow);
  745.                                                     
  746.     // Check if the bounding rects are sane.
  747.     aMovieWidth = aRect.right - aRect.left;
  748.     aMovieHeight = aRect.bottom - aRect.top;
  749.     
  750.     aRect.top = aRect.left  = 0;
  751.     aRect.right = aMovieWidth;
  752.     aRect.bottom = aMovieHeight;
  753.     
  754.     // Resize the window as well.
  755.     SizeWindow(theWindow, aMovieWidth, aMovieHeight, true);
  756.     MoveWindow(theWindow, kDefaultX, kDefaultY, false);                                
  757.  
  758.     SetPort(aSavedPort);
  759.  
  760.     // Add any additional controller functionality.
  761.     AddControllerFunctionality(mc);
  762.  
  763.     // Save important stuff into the window object     and the original size of the movie
  764.     {    
  765.     Rect aRect;
  766.     OSErr anErr;
  767.  
  768.     (**aWindowObject).controller = mc;
  769.     anErr = MCGetControllerBoundsRect(mc, &aRect); DebugAssert(anErr == noErr);
  770.     (**aWindowObject).originalSize = aRect;
  771.     }
  772.     
  773.     return mc;
  774. }
  775.  
  776.  
  777. // ______________________________________________________________________
  778. Boolean DoUpdateMovieFile(WindowRef theWindow)
  779. {
  780.     Movie                     aMovie = NULL;
  781.     WindowObject        aWindowObject = NULL;
  782.     MovieController    mc = NULL;
  783.     OSErr                    anErr;
  784.     
  785.     if ( (theWindow == NULL) || !IsAppWindow(theWindow) )
  786.         return false;
  787.         
  788.     aWindowObject = (WindowObject)GetWRefCon(theWindow); DebugAssert(aWindowObject != NULL);
  789.     mc = (**aWindowObject).controller; DebugAssert(mc != NULL);
  790.     
  791.     if( !(IsWindowObjectOurs(aWindowObject)) && (mc == NULL) )
  792.         return false;
  793.     
  794.     aMovie = MCGetMovie(mc); DebugAssert(aMovie != NULL);
  795.     if(aMovie == NULL)
  796.         return false;
  797.         
  798.     if( (**aWindowObject).FileRefNum == -1)                    // brand new movie, no file attached to it.
  799.     {
  800.         if ( QTUSaveMovie(aMovie) != noErr)
  801.             return false;    
  802.     }
  803.     else                                                                            // we have an existing file, just update the movie resource
  804.     {
  805.         // Open the movie resource file, update the resource, and then close it again!
  806.         anErr = OpenMovieFile(& (**aWindowObject).FileFSSpec, & (**aWindowObject).FileRefNum, fsRdWrPerm);
  807.         DebugAssert(anErr == noErr);
  808.         if(anErr != noErr)
  809.             return false;
  810.         
  811.         anErr = UpdateMovieResource(aMovie, (**aWindowObject).FileRefNum, (**aWindowObject).FileResID, NULL);
  812.         DebugAssert(anErr == noErr);
  813.         
  814.         CloseMovieFile( (**aWindowObject).FileRefNum );
  815.     }
  816.     
  817.     if(anErr == noErr)
  818.         return true;
  819.     else
  820.         return false;
  821. }
  822.  
  823.  
  824. // ______________________________________________________________________
  825. void DoDestroyMovieWindow(WindowRef theWindow)
  826. {
  827.     Movie                     aMovie;
  828.     MovieController    mc;
  829.     WindowObject        aWindowObject;
  830.     
  831.     DebugAssert(theWindow != NULL); if(theWindow == NULL) return;
  832.     
  833.     aWindowObject =(WindowObject)GetWRefCon(theWindow);
  834.     MoveHHi((Handle)aWindowObject);
  835.     HLock((Handle)aWindowObject);
  836.     
  837.     if ( IsWindowObjectOurs(aWindowObject)) // our window?
  838.     {
  839.         mc = (**aWindowObject).controller;
  840.         aMovie = MCGetMovie(mc);
  841.         
  842.         if(aMovie != NULL)
  843.             DisposeMovie(aMovie); DebugAssert(MemError() == noErr);
  844.     
  845.         if(mc != NULL)
  846.             DisposeMovieController(mc); DebugAssert(MemError() == noErr);
  847.     
  848.         if( (**aWindowObject).FileRefNum != -1)
  849.             CloseMovieFile((**aWindowObject).FileRefNum);
  850.         
  851.         (**aWindowObject).ObjectType = NULL;
  852.         (**aWindowObject).controller = NULL;
  853.         (**aWindowObject).FileResID = NULL;
  854.         (**aWindowObject).FileRefNum = NULL;
  855.         
  856.         DisposeHandle((Handle)aWindowObject); DebugAssert(MemError() == noErr);
  857.         DisposeWindow(theWindow); DebugAssert(MemError() == noErr);
  858.         
  859.         CompactMem(0xFFFFFFFF);        //We might as well compact the mem here for getting better performance later.
  860.     }
  861. }
  862.  
  863.  
  864. // ______________________________________________________________________
  865. void DoActivateWindow(WindowRef theWindow, Boolean becomingActive)
  866. {
  867.     WindowObject         aWindowObject = NULL;
  868.     MovieController    mc = NULL;
  869.     GrafPtr                    aSavedPort = NULL;
  870.     
  871.     GetPort(&aSavedPort);
  872.     SetPort((GrafPtr)theWindow);
  873.     
  874.     if( (aWindowObject = (WindowObject)GetWRefCon(theWindow)) != NULL)
  875.     {
  876.         mc = (**aWindowObject).controller;
  877.         if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL) )
  878.             MCActivate(mc, theWindow, becomingActive);
  879.     }
  880.     SetPort(aSavedPort);
  881. }
  882.  
  883.  
  884.  
  885.